home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / multipad.zip / MPFILE.C < prev    next >
Text File  |  1992-07-17  |  13KB  |  396 lines

  1. /***************************************************************************
  2.  *                                                                         *
  3.  *  MODULE    : MpFile.c                                                   *
  4.  *                                                                         *
  5.  *  PURPOSE   : Contains the code for File I/O for Multipad.               *
  6.  *                                                                         *
  7.  *  FUNCTIONS : AlreadyOpen   - Determines if a file is already open.      *
  8.  *                                                                         *
  9.  *              AddFile       - Creates a new MDI window and, if specified,*
  10.  *                loads a file into it.                      *
  11.  *                                                                         *
  12.  *              LoadFile      - Loads a file into a MDI window.            *
  13.  *                                                                         *
  14.  *              ReadFile      - Calls File/Open dialog and appropriately   *
  15.  *                              responds to the user's input.              *
  16.  *                                                                         *
  17.  *              SaveFile      - Saves the contents of a MDI window's edit  *
  18.  *                              control to a file.                         *
  19.  *                                                                         *
  20.  *              SetSaveFrom   - Formats the "Save 'file' to" string.       *
  21.  *                                                                         *
  22.  *              SaveAsDlgProc - Dialog function for the File/SaveAs dialog.*
  23.  *                                                                         *
  24.  *              ChangeFile    - Calls File/SaveAs dialog.                  *
  25.  *                                                                         *
  26.  ***************************************************************************/
  27. // COPYRIGHT:
  28. //
  29. //   (C) Copyright Microsoft Corp. 1992.  All rights reserved.
  30. //
  31. //   You have a royalty-free right to use, modify, reproduce and
  32. //   distribute the Sample Files (and/or any modified version) in
  33. //   any way you find useful, provided that you agree that
  34. //   Microsoft has no warranty obligations or liability for any
  35. //   Sample Application Files which are modified.
  36.  
  37. #include "multipad.h"
  38. #include "commdlg.h"
  39.  
  40. OFSTRUCT    of;
  41. /****************************************************************************
  42.  *                                                                          *
  43.  *  FUNCTION   : AlreadyOpen(szFile)                                        *
  44.  *                                                                          *
  45.  *  PURPOSE    : Checks to see if the file described by the string pointed  *
  46.  *               to by 'szFile' is already open.                            *
  47.  *                                                                          *
  48.  *  RETURNS    : a handle to the described file's window if that file is    *
  49.  *               already open;  NULL otherwise.                             *
  50.  *                                                                          *
  51.  ****************************************************************************/
  52.  
  53. HWND AlreadyOpen(char *szFile)
  54. {
  55.     int     iDiff;
  56.     HWND    hwndCheck;
  57.     char    szChild[64];
  58.     LPSTR   lpChild, lpFile;
  59.     int     wFileTemp;
  60.  
  61.     /* Open the file with the OF_PARSE flag to obtain the fully qualified
  62.      * pathname in the OFSTRUCT structure.
  63.      */
  64.     wFileTemp = OpenFile ((LPSTR)szFile, (LPOFSTRUCT)&of, OF_PARSE);
  65.     if (! wFileTemp)
  66.     return(NULL);
  67.     _lclose (wFileTemp);
  68.  
  69.     /* Check each MDI child window in Multipad */
  70.     for (   hwndCheck = GetWindow(hwndMDIClient, GW_CHILD);
  71.         hwndCheck;
  72.         hwndCheck = GetWindow(hwndCheck, GW_HWNDNEXT)   ) {
  73.     /* Initialization  for comparison */
  74.     lpChild = szChild;
  75.     lpFile = AnsiUpper((LPSTR) of.szPathName);
  76.     iDiff = 0;
  77.  
  78.     /* Skip icon title windows */
  79.     if (GetWindow(hwndCheck, GW_OWNER))
  80.         continue;
  81.  
  82.     /* Get current child window's name */
  83.     GetWindowText(hwndCheck, lpChild, 64);
  84.  
  85.     /* Compare window name with given name */
  86.     while ((*lpChild) && (*lpFile) && (!iDiff)){
  87.         if (*lpChild++ != *lpFile++)
  88.         iDiff = 1;
  89.     }
  90.  
  91.     /* If the two names matched, the file is already   */
  92.     /* open -- return handle to matching child window. */
  93.     if (!iDiff)
  94.         return(hwndCheck);
  95.     }
  96.     /* No match found -- file is not open -- return NULL handle */
  97.     return(NULL);
  98. }
  99.  
  100. /****************************************************************************
  101.  *                                        *
  102.  *  FUNCTION   : AddFile (lpName)                        *
  103.  *                                        *
  104.  *  PURPOSE    : Creates a new MDI window. If the lpName parameter is not   *
  105.  *         NULL, it loads a file into the window.             *
  106.  *                                        *
  107.  *  RETURNS    : HWND  - A handle to the new window.                *
  108.  *                                        *
  109.  ****************************************************************************/
  110.  
  111. HWND FAR PASCAL AddFile(pName)
  112. char * pName;
  113. {
  114.     HWND hwnd;
  115.  
  116.     char        sz[160];
  117.     MDICREATESTRUCT mcs;
  118.  
  119.     if (!pName) {
  120.     /* The pName parameter is NULL -- load the "Untitled" string from */
  121.     /* STRINGTABLE and set the title field of the MDI CreateStruct.    */
  122.     LoadString (hInst, IDS_UNTITLED, sz, sizeof(sz));
  123.     mcs.szTitle = (LPSTR)sz;
  124.     }
  125.     else
  126.     /* Title the window with the fully qualified pathname obtained by
  127.      * calling OpenFile() with the OF_PARSE flag (in function
  128.      * AlreadyOpen(), which is called before AddFile().
  129.      */
  130.     mcs.szTitle = of.szPathName;
  131.  
  132.     mcs.szClass = szChild;
  133.     mcs.hOwner    = hInst;
  134.  
  135.     /* Use the default size for the window */
  136.     mcs.x = mcs.cx = CW_USEDEFAULT;
  137.     mcs.y = mcs.cy = CW_USEDEFAULT;
  138.  
  139.     /* Set the style DWORD of the window to default */
  140.     mcs.style = styleDefault;
  141.  
  142.     /* tell the MDI Client to create the child */
  143.     hwnd = (WORD)SendMessage (hwndMDIClient,
  144.                   WM_MDICREATE,
  145.                   0,
  146.                   (LONG)(LPMDICREATESTRUCT)&mcs);
  147.  
  148.     /* Did we get a file? Read it into the window */
  149.     if (pName){
  150.     if (!LoadFile(hwnd, pName)){
  151.         /* File couldn't be loaded -- close window */
  152.         SendMessage(hwndMDIClient, WM_MDIDESTROY, (WORD) hwnd, 0L);
  153.     }
  154.     }
  155.  
  156.     return hwnd;
  157. }
  158.  
  159. /****************************************************************************
  160.  *                                        *
  161.  *  FUNCTION   : LoadFile (lpName)                        *
  162.  *                                        *
  163.  *  PURPOSE    : Given the handle to a MDI window and a filename, reads the *
  164.  *         file into the window's edit control child.                 *
  165.  *                                        *
  166.  *  RETURNS    : TRUE  - If file is sucessfully loaded.             *
  167.  *         FALSE - Otherwise.                        *
  168.  *                                        *
  169.  ****************************************************************************/
  170.  
  171. int FAR PASCAL LoadFile (hwnd, pName)
  172. HWND hwnd;
  173. char * pName;
  174. {
  175.     WORD   wLength;
  176.     HANDLE hT;
  177.     LPSTR  lpB;
  178.     HWND   hwndEdit;
  179.     int    fh;
  180.  
  181.     hwndEdit = GetWindowWord (hwnd, GWW_HWNDEDIT);
  182.  
  183.     /* The file has a title, so reset the UNTITLED flag. */
  184.     SetWindowWord(hwnd, GWW_UNTITLED, FALSE);
  185.  
  186.     fh = _lopen (pName, 0);
  187.  
  188.     /* Make sure file has been opened correctly */
  189.     if ( fh < 0 )
  190.     goto error;
  191.  
  192.     /* Find the length of the file */
  193.     wLength = (WORD)_llseek (fh, 0L, 2);
  194.     _llseek (fh, 0L, 0);
  195.  
  196.     /* Attempt to reallocate the edit control's buffer to the file size */
  197.     hT = (HANDLE)SendMessage (hwndEdit, EM_GETHANDLE, 0, 0L);
  198.     if (LocalReAlloc(hT, wLength+1, LHND) == NULL) {
  199.     /* Couldn't reallocate to new size -- error */
  200.     _lclose (fh);
  201.     goto error;
  202.     }
  203.  
  204.     /* read the file into the buffer */
  205.     if (wLength != _lread (fh, (lpB = (LPSTR)LocalLock (hT)), wLength))
  206.     MPError (hwnd, MB_OK|MB_ICONHAND, IDS_CANTREAD, (LPSTR)pName);
  207.  
  208.     /* Zero terminate the edit buffer */
  209.     lpB[wLength] = 0;
  210.     LocalUnlock (hT);
  211.  
  212.     SendMessage (hwndEdit, EM_SETHANDLE, hT, 0L);
  213.     _lclose (fh);
  214.  
  215.     return TRUE;
  216.  
  217. error:
  218.     /* Report the error and quit */
  219.     MPError(hwnd, MB_OK | MB_ICONHAND, IDS_CANTOPEN, (LPSTR)pName);
  220.     return FALSE;
  221. }
  222.  
  223. /****************************************************************************
  224.  *                                                                          *
  225.  *  FUNCTION   : ReadFile(hwnd)                                             *
  226.  *                                                                          *
  227.  *  PURPOSE    : Called in response to a File/Open menu selection. It asks  *
  228.  *               the user for a file name and responds appropriately.       *
  229.  *                                                                          *
  230.  ****************************************************************************/
  231.  
  232. VOID FAR PASCAL ReadFile(HWND hwnd)
  233. {
  234.     char    szFile[128];
  235.     HWND    hwndFile;
  236.     OPENFILENAME of;
  237.  
  238.     lstrcpy(szFile, "*.TXT");
  239.  
  240.     of.lStructSize  = sizeof(OPENFILENAME);
  241.     of.hwndOwner    =(HWND) hwnd;
  242.     of.hInstance    = (HANDLE)NULL;
  243.     of.lpstrFilter  = (LPSTR)"Text Files (*.TXT)\0*.TXT\0";
  244.     of.lpstrCustomFilter = (LPSTR)NULL;
  245.     of.nMaxCustFilter     = 0L;
  246.     of.nFilterIndex = 1;
  247.     of.lpstrFile    = (LPSTR)szFile;
  248.     of.nMaxFile     = 128;
  249.     of.lpstrFileTitle     = (LPSTR)NULL;
  250.     of.nMaxFileTitle     = 0;
  251.     of.lpstrInitialDir = (LPSTR)NULL;
  252.     of.lpstrTitle   = NULL;
  253.     of.Flags        = OFN_HIDEREADONLY|OFN_FILEMUSTEXIST;
  254.     of.lpstrDefExt  = NULL;
  255.     of.nFileOffset     = 0;
  256.     of.nFileExtension     = 0;
  257.     of.lpstrDefExt     = (LPSTR)NULL;
  258.     of.lCustData     = 0L;
  259.     of.lpTemplateName     = (LPSTR)NULL;
  260.  
  261.     if(!GetOpenFileName(&of))
  262.        return;
  263.  
  264.     /* If the result is not the empty string -- take appropriate action */
  265.     if (*szFile) {
  266.          /* Is file already open?? */
  267.          if (hwndFile = AlreadyOpen(szFile)) {
  268.             /* Yes -- bring the file's window to the top */
  269.             BringWindowToTop(hwndFile);
  270.          }
  271.          else {
  272.             /* No -- make a new window and load file into it */
  273.             AddFile(szFile);
  274.          }
  275.     }
  276. }
  277.  
  278. /****************************************************************************
  279.  *                                        *
  280.  *  FUNCTION   : SaveFile (hwnd)                        *
  281.  *                                        *
  282.  *  PURPOSE    : Saves contents of current edit control to disk.        *
  283.  *                                        *
  284.  ****************************************************************************/
  285.  
  286. VOID FAR PASCAL SaveFile( hwnd )
  287.  
  288. HWND hwnd;
  289. {
  290.     HANDLE   hT;
  291.     LPSTR    lpT;
  292.     char     szFile[128];
  293.     WORD     cch;
  294.     int      fh;
  295.     OFSTRUCT of;
  296.     HWND     hwndEdit;
  297.  
  298.     hwndEdit = GetWindowWord ( hwnd, GWW_HWNDEDIT);
  299.     GetWindowText (hwnd, szFile, sizeof(szFile));
  300.  
  301.     /* If there is no extension (control is 'Untitled') add .TXT as extension */
  302.     for (cch = FALSE, lpT = szFile; *lpT; lpT++)
  303.     switch (*lpT){
  304.         case '.':
  305.          cch = TRUE;
  306.          break;
  307.  
  308.         case '\\':
  309.         case ':' :
  310.          cch = FALSE;
  311.          break;
  312.     }
  313.     if (!cch)
  314.     LoadString (hInst, IDS_ADDEXT, lpT, lpT - (LPSTR)szFile);
  315.  
  316.     fh = OpenFile (szFile, &of, OF_WRITE | OF_CREATE);
  317.  
  318.     /* If file could not be opened, quit */
  319.     if (fh < 0){
  320.     MPError (hwnd, MB_OK | MB_ICONHAND, IDS_CANTCREATE, (LPSTR)szFile);
  321.     return;
  322.     }
  323.  
  324.     /* Find out the length of the text in the edit control */
  325.     cch = GetWindowTextLength (hwndEdit);
  326.  
  327.     /* Obtain a handle to the text buffer */
  328.     hT    = (HANDLE)SendMessage (hwndEdit, EM_GETHANDLE, 0, 0L);
  329.     lpT = (LPSTR)LocalLock (hT);
  330.  
  331.     /* Write out the contents of the buffer to the file. */
  332.     if (cch != _lwrite (fh, lpT, cch))
  333.     MPError (hwnd, MB_OK | MB_ICONHAND, IDS_CANTWRITE, (LPSTR)szFile);
  334.  
  335.     /* Clean up */
  336.     LocalUnlock (hT);
  337.     SendMessage (hwndEdit, EM_SETHANDLE, hT, 0L);
  338.  
  339.     _lclose (fh);
  340.  
  341.     return;
  342. }
  343.  
  344.  
  345. /****************************************************************************
  346.  *                                        *
  347.  *  FUNCTION   : ChangeFile (hwnd)                        *
  348.  *                                        *
  349.  *  PURPOSE    : Invokes the File/SaveAs dialog.                *
  350.  *                                        *
  351.  *  RETURNS    : TRUE  - if user selected OK or NO.                *
  352.  *         FALSE - otherwise.                        *
  353.  *                                        *
  354.  ****************************************************************************/
  355.  
  356. BOOL FAR PASCAL ChangeFile (hwnd)
  357. HWND hwnd;
  358. {
  359.     char    szFile[128];
  360.     OPENFILENAME of;
  361.  
  362.     if (GetWindowWord(hwnd, GWW_UNTITLED))
  363.         lstrcpy(szFile, "*.TXT");
  364.     else
  365.         GetWindowText(hwnd, szFile, 128);
  366.  
  367.     of.lStructSize  = sizeof(OPENFILENAME);
  368.     of.hwndOwner    =(HWND) hwnd;
  369.     of.hInstance    = (HANDLE)NULL;
  370.     of.lpstrFilter  = (LPSTR)"Text Files (*.TXT)\0*.TXT\0";
  371.     of.lpstrCustomFilter = (LPSTR)NULL;
  372.     of.nMaxCustFilter     = 0L;
  373.     of.nFilterIndex = 1;
  374.     of.lpstrFile    = (LPSTR)szFile;
  375.     of.nMaxFile     = 128;
  376.     of.lpstrFileTitle     = (LPSTR)NULL;
  377.     of.nMaxFileTitle     = 0;
  378.     of.lpstrInitialDir = (LPSTR)NULL;
  379.     of.lpstrTitle   = NULL;
  380.     of.Flags        = OFN_HIDEREADONLY;
  381.     of.lpstrDefExt  = NULL;
  382.     of.nFileOffset     = 0;
  383.     of.nFileExtension     = 0;
  384.     of.lpstrDefExt     = (LPSTR)NULL;
  385.     of.lCustData     = 0L;
  386.     of.lpTemplateName     = (LPSTR)NULL;
  387.  
  388.     if(!GetSaveFileName(&of))
  389.        return(FALSE);
  390.  
  391.     SetWindowWord(hwnd, GWW_UNTITLED, 0);
  392.     SetWindowText(hwnd, szFile);
  393.     return(TRUE);
  394. }
  395. 
  396.